package com.sywl.orm;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;


@Entity
@Table(name="sys_account")
public class Account {
	
	private Integer accId;
	private String accCode;
	private String accName;
	private Set<Role> roles=new HashSet<Role>(0);
	
	public Account() {
		// TODO Auto-generated constructor stub
	}
	
	public Account(String accCode, String accName) {
		this.accCode = accCode;
		this.accName = accName;
	}
	public Account(String accCode, String accName, Set<Role> roles) {
		this.accCode = accCode;
		this.accName = accName;
		this.roles = roles;
	}
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="ACCID",unique=true,nullable=false)
	public Integer getAccId() {
		return accId;
	}
	public void setAccId(Integer accId) {
		this.accId = accId;
	}
	@Column(name="ACCCODE",unique=true,nullable=false)
	public String getAccCode() {
		return accCode;
	}
	public void setAccCode(String accCode) {
		this.accCode = accCode;
	}
	@Column(name="ACCNAME",unique=true,nullable=false)
	public String getAccName() {
		return accName;
	}
	public void setAccName(String accName) {
		this.accName = accName;
	}
//	@JsonManagedReference
	@JsonIgnore
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name="sys_account_role",joinColumns={
			@JoinColumn(name="ACCID",nullable=false,updatable=false)},
			inverseJoinColumns={@JoinColumn(name="ROLEID",nullable=false,updatable=false)})
	public Set<Role> getRoles() {
		return roles;
	}
	public void setRoles(Set<Role> roles) {
		System.out.println("setRoles:"+roles);
		this.roles = roles;
	}
	
	/*public void setRoles(Object roles) {
		ArrayList<Role> rr=null;
		if(roles instanceof String){
			rr=new ArrayList();
			Role role=new Role();
			role.setRoleId(Integer.parseInt(roles.toString()));
			rr.add(role);
			this.roles=rr;
		}else{
			this.roles=(Set<Role>)roles;
		}
	}*/
}
